﻿using System;
using System.Collections.Generic;

namespace IupNET
{
    /// <summary>
    /// This class is used to get a common base class for diffrent items that can be in a menu
    /// </summary>
    public class MenuElement : Control
    {
        public MenuElement(IntPtr obj)
            : base(obj)
        {
        }
    }

    public class MenuItem:MenuElement
    {
        public MenuItem()
            : base(Iupc.Item(null,null))
        {

        }

       
        public bool AutoToggle
        {
            get
            {
                return GetAttribute("AUTOTOGGLE") == "YES";
            }
            set
            {
                SetAttribute("AUTOTOGGLE", value ? "YES" : "NO");
            }
        }

        public bool HideMark
        {
            get
            {
                return GetAttribute("HIDEMARK") == "YES";
            }
            set
            {
                SetAttribute("HIDEMARK", value ? "YES" : "NO");
            }
        }

        public Image Image
        {
            set
            {
                string n = value.HandleName;
                if (n == null)
                    return;
                SetAttribute("IMAGE", n);
            }

        }

        public Image ImPress
        {
            set
            {
                string n = value.HandleName;
                if (n == null)
                    return;
                SetAttribute("IMPRESS", n);
            }
        }

        public Image TitleImage
        {
            set
            {
                string n = value.HandleName;
                if (n == null)
                    return;
                SetAttribute("TITLEIMAGE", n);
            }
        }

        public new bool Value
        {
            get
            {
                return GetAttribute("VALUE") == "ON";
            }
            set
            {
                SetAttribute("VALUE", value ? "ON" : "OFF");
                
            }
        }

        public EventCallback Action
        {
            get
            {
                return CBHandler.Fetch("ACTION_ITEM");
            }
            set
            {
                CBHandler.Set("ACTION_ITEM", value);
            }
        }

        public EventCallback HighlightCB
        {
            get
            {
                return CBHandler.Fetch("HIGHLIGHT_CB");
            }
            set
            {
                CBHandler.Set("HIGHLIGHT_CB", value);
            }
        }
        
    }

    public class Separator : MenuElement
    {
        public Separator()
            : base(Iupc.Separator())
        {

        }

    }

    public class Submenu : MenuElement
    {
      

        public Submenu(Menu child)
            : base(Iupc.Submenu(null, child.cobject))
        {

        }


        public Image Image
        {
            set
            {
                string n = value.HandleName;
                if (n == null)
                    return;
                SetAttribute("IMAGE", n);
            }

        }

        public new string Title
        {
            //overridden for documentation purposes
            get
            {
                return GetAttribute("TITLE");
            }
            set
            {
                SetAttribute("TITLE", value);
            }
        }

        public EventCallback HighlightCB
        {
            get
            {
                return CBHandler.Fetch("HIGHLIGHT_CB");
            }
            set
            {
                CBHandler.Set("HIGHLIGHT_CB", value);
            }
        }

       
    }

    public class Menu:Control
    {
        public Menu(params MenuElement[] items):base(IntPtr.Zero) {
            List<IntPtr> ptrs=new List<IntPtr>();
            foreach (MenuElement me in items)
                ptrs.Add(me.cobject);
            ptrs.Add(IntPtr.Zero);
            Initialize(Iupc.Menuv(ptrs.ToArray()));
        }

        public bool Radio
        {
            get
            {
                return GetAttribute("RADIO") == "YES";
            }
            set
            {
                SetAttribute("VALUE", value ? "YES" : "NO");

            }
        }

        public void Popup(int x, int y)
        {
            Iupc.Popup(cobject, x, y);
        }

        public void Append(MenuElement me)
        {
            Iupc.Append(cobject, me.cobject);
        }

        public EventCallback OpenCB
        {
            get
            {
                return CBHandler.Fetch("OPEN_CB");
            }
            set
            {
                CBHandler.Set("OPEN_CB", value);
            }
        }

        public EventCallback MenuCloseCB
        {
            get
            {
                return CBHandler.Fetch("MENUCLOSE_CB");
            }
            set
            {
                CBHandler.Set("MENUCLOSE_CB", value);
            }
        }
    }
}
